//
//  VideoDetailVew.swift
//  TestConcurreces
//
//  Created by rolodestar on 2025/6/16.
//

import SwiftUI
import SwiftData
import AVKit
import RoSwiftDataForNetVideoDownload
struct VideoDetailVew: View {
    @EnvironmentObject var actor: VideoActor
    var video: BeautyModel
    var body: some View {
        VStack{
            if(video.downloadProgress > 0 && video.downloadProgress < 1){
                ProgressView(value: video.downloadProgress, total: 1)
            }
            if(video.localUrl == nil){
                
                VideoPlayer(player: AVPlayer(url: video.remoteUrl))
                    .toolbar {
                        ToolbarItem {
                            Button("下载"){
                                Task(priority: .background ){
                                    try await actor.startDownload(id: video.persistentModelID)
                                }
                            }
                        }
                    }
                    .toolbar {
                        ToolbarItem {
                            if(video.downloadProgress > 0 && video.downloadProgress < 1){
                                Button("暂停"){
                                    Task(priority: .background ){
                                        await actor.pauseDownload(id: video.persistentModelID)
                                    }
                                }
                            }
                        }
                    }
                    .toolbar {
                        ToolbarItem {
                            if(video.downloadProgress > 0 && video.downloadProgress < 1 && video.resumeData != nil){
                                Button("恢复"){
                                    Task(priority: .background ){
                                        await actor.resumeDownload(id: video.persistentModelID)
                                    }
                                }
                            }
                        }
                    }
            }else{
                VideoPlayer(player: AVPlayer(url: video.localUrl!))
            }
        }
    }
}

#Preview {
    VideoDetailVew(video: BeautyModel.debugVideo)
}
